package qldc.pm.tiensu.searchcontainer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;

import qldc.pm.tiensu.constants.TienSuConstants;
import qldc.pm.tiensu.dto.DS_NguoiTienSuDTO;
import qldc.pm.tiensu.dto.DanhSachTienSuDTO;
import qldc.pm.tiensu.searchcontainer.util.nguoicanxoaantich.DoiTuongTienSuCanXoaAnTichSearchUtil;
import qldc.pm.tiensu.searchcontainer.util.nguoicotiensu.DoiTuongTienSuSearchUtil;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class TienSuCanXoaAnTichSearch extends SearchContainer<DS_NguoiTienSuDTO> {
	public static final String EMPTY_RESULTS_MESSAGE = "khong-tim-thay-doi-tuong-nao";
	static List<String> headerNames = new ArrayList<String>();
	static Map<String, String> orderableHeaders = new HashMap<String, String>();
	static {
	
		orderableHeaders.put("ho-ten","ho-ten");
		orderableHeaders.put("so-dinh-danh-ca-nhan","so-dinh-danh-ca-nhan");
	
	}
	public TienSuCanXoaAnTichSearch(PortletRequest portletRequest, PortletURL iteratorURL) {

		super(portletRequest, new TienSuCanXoaAnTichDisplayTerms(portletRequest),
				new TienSuCanXoaAnTichSearchTerms(portletRequest),
				DEFAULT_CUR_PARAM, DEFAULT_DELTA, iteratorURL, headerNames,
				EMPTY_RESULTS_MESSAGE);

		TienSuCanXoaAnTichDisplayTerms displayTerms = (TienSuCanXoaAnTichDisplayTerms) getDisplayTerms();

		SimpleDateFormat dateFormat = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);


		if (displayTerms.getTuNgayToaAn() != null) {
			iteratorURL.setParameter(TienSuCanXoaAnTichSearchTerms.TU_NGAY_TOA_AN,
					dateFormat.format(displayTerms.getTuNgayToaAn()));
		}

		if (displayTerms.getDenNgayToaAn() != null) {
			iteratorURL.setParameter(TienSuCanXoaAnTichSearchTerms.DEN_NGAY_TOA_AN,
					dateFormat.format(displayTerms.getDenNgayToaAn()));
		}
		if (displayTerms.getTuNgayThongKe() != null) {
			iteratorURL.setParameter(TienSuCanXoaAnTichSearchTerms.TU_NGAY_THONG_KE,
					dateFormat.format(displayTerms.getTuNgayThongKe()));
		}

		if (displayTerms.getDenNgayThongKe() != null) {
			iteratorURL.setParameter(TienSuCanXoaAnTichSearchTerms.DEN_NGAY_THONG_KE,
					dateFormat.format(displayTerms.getDenNgayThongKe()));
		}
		if (displayTerms.getTuNgayXoaAnTich() != null) {
			iteratorURL.setParameter(TienSuCanXoaAnTichSearchTerms.TU_NGAY_XOA_AN_TICH,
					dateFormat.format(displayTerms.getTuNgayXoaAnTich()));
		}

		if (displayTerms.getDenNgayXoaAnTich() != null) {
			iteratorURL.setParameter(TienSuCanXoaAnTichSearchTerms.DEN_NGAY_XOA_AN_TICH,
					dateFormat.format(displayTerms.getDenNgayXoaAnTich()));
		}
		iteratorURL.setParameter(TienSuCanXoaAnTichDisplayTerms.TRANG_THAI,
				String.valueOf(displayTerms.getTrangThai()));
		iteratorURL.setParameter(TienSuCanXoaAnTichDisplayTerms.TINH_ID,
				String.valueOf(displayTerms.getTinhId()));
		iteratorURL.setParameter(TienSuCanXoaAnTichDisplayTerms.QUAN_HUYEN_ID,
				String.valueOf(displayTerms.getQuanHuyenId()));
		iteratorURL.setParameter(TienSuCanXoaAnTichDisplayTerms.PHUONG_XA_ID,
				String.valueOf(displayTerms.getPhuongXaId()));
		try {
			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(portletRequest);

			String orderByCol = ParamUtil.getString(portletRequest, "orderByCol");
			String orderByType = ParamUtil.getString(portletRequest,"orderByType");
			String portletName = "tiensu";

			if(Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)){
				preferences.setValue(portletName, "order-by-col", orderByCol);
				preferences.setValue(portletName, "order-by-type", orderByType);

			} else{
				orderByCol = preferences.getValue(portletName, "order-by-col", "");
				orderByType = preferences.getValue(portletName, "order-by-type", "asc");
			}

			OrderByComparator orderByComparator = DoiTuongTienSuCanXoaAnTichSearchUtil.getDoiTuongTienSuCanXoaAnTichOrderByComparator(orderByCol, orderByType);

			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		} catch (SystemException e) {
			System.out.println("E "+e.getMessage());
 		}
	}
}
